﻿Public Class LABORDER
    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub LABORDER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        editLab_order.Enabled = False
        addLabitem.Enabled = False
        editLabitem.Enabled = False

        Dim objSelectDataLabOrder As New LABORDER
        Dim labOrderData As DataTable
        labOrderData = connect.GetTable(objSelectDataLabOrder.selectLabOrder_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        lab_orderGrid.PrimaryGrid.DataSource = labOrderData
        objSelectDataLabOrder.comboboxData("SELECT lab_group_code AS 'a', lab_group_name AS 'b' FROM lab_group", labgroupCmb)
        objSelectDataLabOrder.comboboxData("SELECT `ccode` AS 'a', `name` AS 'b' FROM finchcode WHERE `status` = 1 ", CCODECmb)

        DesignForm(Me)
        DesignGrid(lab_orderGrid)
        DesignGrid(lab_itemGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub CCODECmb_SelectedValueChanged(sender As Object, e As EventArgs) Handles CCODECmb.SelectedValueChanged
        If CCODECmb.SelectedValue Is Nothing Then
            Return
        End If
        If CCODECmb.SelectedValue.ToString.Trim = "System.Data.DataRowView" Then
            Return
        End If
        Dim sql As String
        Dim FINCHCODE As New DataTable
        sql = "Select `rcode`,`name` FROM finrecode WHERE `ccode` = '" & CCODECmb.SelectedValue & "'"
        FINCHCODE = connect.GetTable(sql)
        If FINCHCODE.Rows.Count > 0 Then
            Try
                With RCODECmb
                    .DataSource = FINCHCODE
                    .DisplayMember = "name"
                    .ValueMember = "rcode"
                End With
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        Else
            RCODECmb.DataSource = Nothing
            RCODECmb.Text = Nothing
        End If
    End Sub

    Private Sub lab_orderGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles lab_orderGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        codeTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส lab order").Value()
        lab_order_codeTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส lab order").Value()
        prdcodeTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผลิตภัณฑ์").Value()
        nameTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อการตรวจสอบ lab").Value()
        short_nameTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อการตรวจสอบย่อ").Value()
        labgroupCmb.SelectedValue = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่ม lab").Value()

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("พิมพ์/ไม่พิมพ์ Barcode").Value() = True Then
            print_barcodeChk.Checked = True
        Else
            print_barcodeChk.Checked = False
        End If

        tat_limit_dayTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เวลาในการรอผล lab/วัน").Value()
        tat_limit_hourTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เวลาในการรอผล lab/ชั่วโมง").Value()
        tat_limit_minTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เวลาในการรอผล lab/นาที").Value()

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value().ToString.Length > 0 Then
            commentLabOrderTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value()
        Else
            commentLabOrderTxt.Text = ""
        End If

        CCODECmb.SelectedValue = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CCODE").Value()
        RCODECmb.SelectedValue = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("RCODE").Value()

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("Finance_PRC").Value() = True Then
            F_PRCStatusChk.Checked = True
        Else
            F_PRCStatusChk.Checked = False
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            labOrderStatusChk.Checked = True
        Else
            labOrderStatusChk.Checked = False
        End If
        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("lab_order", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "code", codeTxt.Text)

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ยกเลิกการร้องขอ lab นี้").Value() = True Then
            CANCEL_ORDERStatusChk.Checked = True
        Else
            CANCEL_ORDERStatusChk.Checked = False
        End If
        objChkboxUpdate.clickChkboxUpdate("lab_order", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ยกเลิกการร้องขอ lab นี้").Value(), "CANCEL_ORDER", "code", codeTxt.Text)

        Dim objLookupLabitem As New LABITEM
        Dim labItemData As DataTable
        labItemData = connect.GetTable(objLookupLabitem.lookupLabitem(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส lab order").Value()))
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        lab_itemGrid.PrimaryGrid.DataSource = labItemData

        editLab_order.Enabled = True
        addLabitem.Enabled = True
    End Sub

    Private Sub lab_itemGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles lab_itemGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        labitemCodeTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส lab item").Value()
        lab_order_codeTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส lab order").Value()
        labItemNameTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อ lab item").Value()
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หน่วย").Value().ToString.Length > 0 Then
            unitLabItemTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หน่วย").Value()
        Else
            unitLabItemTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("normal").Value().ToString.Length > 0 Then
            normalTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("normal").Value()
        Else
            normalTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value().ToString.Length > 0 Then
            labitemCommentTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value()
        Else
            labitemCommentTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("f cancel").Value() = True Then
            labitemFcancelStatusChk.Checked = True
        Else
            labitemFcancelStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("lab_item", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("f cancel").Value(), "f_cancel", "code", labitemCodeTxt.Text)

        editLabitem.Enabled = True
    End Sub

    Private Sub addLab_order_Click(sender As Object, e As EventArgs) Handles addLab_order.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If nameTxt.Text.ToString.Length > 0 And labgroupCmb.SelectedValue.ToString.Length > 0 Then
                    Dim objInsertData As New LABORDER
                    objInsertData.name_ = nameTxt.Text
                    objInsertData.short_name_ = short_nameTxt.Text
                    objInsertData.lab_group_code_ = labgroupCmb.SelectedValue
                    objInsertData.print_barcode_ = print_barcodeChk.Checked
                    objInsertData.tat_limit_day_ = tat_limit_dayTxt.Text
                    objInsertData.tat_limit_hour_ = tat_limit_hourTxt.Text
                    objInsertData.tat_limit_min_ = tat_limit_minTxt.Text
                    objInsertData.comment_ = commentLabOrderTxt.Text
                    objInsertData.STATUS_ = labOrderStatusChk.Checked
                    objInsertData.CANCEL_ORDER_ = CANCEL_ORDERStatusChk.Checked

                    objInsertData.CCODE_ = CCODECmb.SelectedValue
                    objInsertData.RCODE_ = RCODECmb.SelectedValue
                    objInsertData.F_PRC_ = F_PRCStatusChk.Checked
                    objInsertData.insertLab_orderData()

                    Dim objSelectDataLabOrder As New LABORDER
                    Dim labOrderData As DataTable
                    labOrderData = connect.GetTable(objSelectDataLabOrder.selectLabOrder_)
                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                    lab_orderGrid.PrimaryGrid.DataSource = labOrderData

                    codeTxt.Text = ""
                    prdcodeTxt.Text = ""
                    labitemCodeTxt.Text = ""
                    nameTxt.Text = ""
                    short_nameTxt.Text = ""
                    print_barcodeChk.Checked = False
                    tat_limit_dayTxt.Text = ""
                    tat_limit_hourTxt.Text = ""
                    tat_limit_minTxt.Text = ""
                    commentLabOrderTxt.Text = ""
                    F_PRCStatusChk.Checked = False
                    labOrderStatusChk.Checked = False
                    CANCEL_ORDERStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editLab_order_Click(sender As Object, e As EventArgs) Handles editLab_order.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If nameTxt.Text.ToString.Length > 0 And labgroupCmb.SelectedValue.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูล lab order ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New LABORDER
                        objEditData.code_ = codeTxt.Text
                        objEditData.PRDCODE_ = prdcodeTxt.Text
                        objEditData.name_ = nameTxt.Text
                        objEditData.short_name_ = short_nameTxt.Text
                        objEditData.lab_group_code_ = labgroupCmb.SelectedValue
                        objEditData.print_barcode_ = print_barcodeChk.Checked
                        objEditData.tat_limit_day_ = tat_limit_dayTxt.Text
                        objEditData.tat_limit_hour_ = tat_limit_hourTxt.Text
                        objEditData.tat_limit_min_ = tat_limit_minTxt.Text
                        objEditData.comment_ = commentLabOrderTxt.Text
                        objEditData.STATUS_ = labOrderStatusChk.Checked
                        objEditData.CANCEL_ORDER_ = CANCEL_ORDERStatusChk.Checked

                        objEditData.CCODE_ = CCODECmb.SelectedValue
                        objEditData.RCODE_ = RCODECmb.SelectedValue
                        objEditData.F_PRC_ = F_PRCStatusChk.Checked

                        objEditData.editLab_orderData()

                        Dim objSelectDataLabOrder As New LABORDER
                        Dim labOrderData As DataTable
                        labOrderData = connect.GetTable(objSelectDataLabOrder.selectLabOrder_)
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        lab_orderGrid.PrimaryGrid.DataSource = labOrderData

                        codeTxt.Text = ""
                        prdcodeTxt.Text = ""
                        labitemCodeTxt.Text = ""
                        nameTxt.Text = ""
                        short_nameTxt.Text = ""
                        print_barcodeChk.Checked = False
                        tat_limit_dayTxt.Text = ""
                        tat_limit_hourTxt.Text = ""
                        tat_limit_minTxt.Text = ""
                        commentLabOrderTxt.Text = ""
                        F_PRCStatusChk.Checked = False
                        labOrderStatusChk.Checked = False
                        CANCEL_ORDERStatusChk.Checked = False
                        editLab_order.Enabled = False
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub addLabitem_Click(sender As Object, e As EventArgs) Handles addLabitem.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If lab_order_codeTxt.Text.ToString.Length > 0 And labItemNameTxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New LABITEM
                    objInsertData.lab_order_code_ = lab_order_codeTxt.Text
                    objInsertData.name_ = labItemNameTxt.Text
                    objInsertData.unit_ = unitLabItemTxt.Text
                    objInsertData.normal_ = normalTxt.Text
                    objInsertData.comment_ = labitemCommentTxt.Text
                    objInsertData.f_cancel_ = labitemFcancelStatusChk.Checked
                    objInsertData.insertLabItemData()

                    Dim objSelectDataLabItem As New LABITEM
                    Dim labItemData As DataTable
                    labItemData = connect.GetTable(objSelectDataLabItem.lookupLabitem(lab_order_codeTxt.Text))
                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                    lab_itemGrid.PrimaryGrid.DataSource = labItemData

                    labitemCodeTxt.Text = ""
                    lab_order_codeTxt.Text = ""
                    labItemNameTxt.Text = ""
                    unitLabItemTxt.Text = ""
                    normalTxt.Text = ""
                    labitemCommentTxt.Text = ""
                    labitemFcancelStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editLabitem_Click(sender As Object, e As EventArgs) Handles editLabitem.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If lab_order_codeTxt.Text.ToString.Length > 0 And labItemNameTxt.Text.ToString.Length > 0 And labitemCodeTxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูล lab item ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New LABITEM
                        objEditData.code_ = labitemCodeTxt.Text
                        objEditData.lab_order_code_ = lab_order_codeTxt.Text
                        objEditData.name_ = labItemNameTxt.Text
                        objEditData.unit_ = unitLabItemTxt.Text
                        objEditData.normal_ = normalTxt.Text
                        objEditData.comment_ = labitemCommentTxt.Text
                        objEditData.f_cancel_ = labitemFcancelStatusChk.Checked
                        objEditData.editLabItemData()

                        Dim objSelectDataLabItem As New LABITEM
                        Dim labItemData As DataTable
                        labItemData = connect.GetTable(objSelectDataLabItem.lookupLabitem(lab_order_codeTxt.Text))
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        lab_itemGrid.PrimaryGrid.DataSource = labItemData

                        labitemCodeTxt.Text = ""
                        lab_order_codeTxt.Text = ""
                        labItemNameTxt.Text = ""
                        unitLabItemTxt.Text = ""
                        normalTxt.Text = ""
                        labitemCommentTxt.Text = ""
                        labitemFcancelStatusChk.Checked = False
                        editLabitem.Enabled = True
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("labgroup")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("labgroup")
        nextform.ShowDialog()
    End Sub
End Class